package com.arunsawad.ilertu.activity;

import android.app.Activity;
import android.app.AlertDialog;
import android.app.ProgressDialog;
import android.app.AlertDialog.Builder;
import android.content.DialogInterface;
import android.content.DialogInterface.OnClickListener;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.view.View;
import android.view.Window;
import android.widget.ImageButton;
import android.widget.TextView;

import com.arunsawad.ilertu.R;
import com.arunsawad.ilertu.common.AsyncActivity;
import com.arunsawad.ilertu.common.Constants;
import com.arunsawad.ilertu.common.ILertU;
import com.arunsawad.ilertu.common.RequestTask;
import com.arunsawad.ilertu.common.Validator;

public class Home extends Activity implements AsyncActivity {
	SharedPreferences preferences;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		requestWindowFeature(Window.FEATURE_CUSTOM_TITLE);
		setContentView(R.layout.act_home);
		getWindow().setFeatureInt(Window.FEATURE_CUSTOM_TITLE,
				R.layout.title0_imgbtn_txt);
		
		initTitle();
		
		preferences = getSharedPreferences(Constants.PREF_FILE, MODE_PRIVATE);
		Validator.Validate(this, preferences);
	}
	
	public void initTitle() {
		ImageButton imgBtn1 = (ImageButton) findViewById(R.id.title0_imgBtn1);
		imgBtn1.setOnClickListener(new View.OnClickListener() {
			public void onClick(View v) {
				Intent intent = new Intent(getApplicationContext(), Home.class);
				startActivity(intent);
			}
		});

		TextView txtTitle = (TextView) findViewById(R.id.title0_txtTitle);
		txtTitle.setText(getString(R.string.home));
	}

	public void showAccOptions(View view) {
		AlertDialog.Builder alertDialogBuilder = new Builder(this);
		alertDialogBuilder.setTitle(getString(R.string.account_options));
		String[] types = { getString(R.string.edit_profile),
				getString(R.string.change_password),
				getString(R.string.sign_out), getString(R.string.cancel) };
		alertDialogBuilder.setItems(types, new OnClickListener() {
			@Override
			public void onClick(DialogInterface dialog, int which) {
				dialog.dismiss();
				Intent intent;
				switch (which) {
				case 0: // edit profile
					intent = new Intent(Home.this, EditProfile.class);
					((ILertU) Home.this.getApplication()).setImagePath("");
					startActivity(intent);
					break;
				case 1: // change password
					intent = new Intent(Home.this, ChangePassword.class);
					startActivity(intent);
					break;
				case 2: // sign out
					AlertDialog.Builder alertDialogBuilder = new AlertDialog.Builder(
							Home.this);
					alertDialogBuilder
							.setMessage(R.string.confirm_signout)
							.setPositiveButton(R.string.ok,
									new DialogInterface.OnClickListener() {
										public void onClick(
												DialogInterface dialog, int id) {
											ProgressDialog bar = new ProgressDialog(Home.this);
											bar.setIndeterminate(true);
											bar.show();
											
											SharedPreferences.Editor editor = preferences
													.edit();
											editor.remove(Constants.PREF_USER_ID);
											editor.remove(Constants.PREF_USER_NAME);
											editor.remove(Constants.PREF_PASSWORD);
											editor.commit();

											String msg = String
													.format(Constants.MSG,
															"ilertu.user-sign-out",
															preferences
																	.getString(
																			Constants.PREF_USER_NAME,
																			""),
															preferences
																	.getString(
																			Constants.PREF_PASSWORD,
																			""),
															preferences
																	.getString(
																			Constants.PREF_LANGUAGE,
																			"en"),
															"");

											RequestTask task = new RequestTask();
											task.setListener(Home.this);
											task.execute(
													Constants.URL_SIGN_OUT, msg);
										}
									})
							.setNegativeButton(R.string.cancel,
									new DialogInterface.OnClickListener() {
										public void onClick(
												DialogInterface dialog, int id) {
											dialog.cancel();
										}
									});
					AlertDialog alert = alertDialogBuilder.create();
					alert.show();
					break;
				case 3:
					dialog.cancel();
					break;
				}
			}
		});
		alertDialogBuilder.show();
	}

	public void editSettings(View view) {
		Intent myIntent = new Intent(this, Settings.class);
		startActivity(myIntent);
	}

	@Override
	public void onRequestCompleted(String results) {
		Intent myIntent = new Intent(this, Index.class);
		startActivity(myIntent);
	}

	public void viewEmegencies(View view) {
		Intent intent = new Intent(this, Emergencies.class);
		startActivity(intent);
	}

	public void viewRegisteredServices(View view) {
		Intent intent = new Intent(this, RegisteredServices.class);
		startActivity(intent);
	}

	public void viewLocation(View view) {
		Intent intent = new Intent(this, WhereRU.class);
		startActivity(intent);
	}
}
